package com.nsn.das.report.utils;


import java.util.List;
import java.util.ArrayList;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import java.util.Enumeration;
import org.apache.commons.beanutils.PropertyUtils;

import com.nsn.das.report.beans.ChangeDescription;


public class ObjectContentCmpUtils {
	/*
	 * Get list of property by reading the property file
	 * @param filePath
	 * @return List<String>
	 */
	private static List<String> readProperties(String filePath){
		List<String> properslist = new ArrayList<String>();
		Properties props = new Properties();
        FileInputStream fis = null;
        BufferedInputStream in = null;
		try {
			fis = new FileInputStream(filePath);
			in = new BufferedInputStream(fis);
			props.load(in);
			Enumeration en = props.propertyNames();
			while (en.hasMoreElements()){
				String key = (String)en.nextElement();
				String property = props.getProperty(key);
				properslist.add(property);
            }
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			if (in != null) {
				try {
					in.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
			if (fis != null) {
				try {
					fis.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
		}
		return properslist;
	}
		
	/*
	 * Get list of ChangeDescription between two different CommandData object
	 * @param olddata, newdata
	 * @return null, list<ChanngeDescription>
	 */
	public static List<ChangeDescription> compareCommandData(Object olddata, Object newdata) throws IllegalArgumentException{
		if ( olddata.equals(null) || newdata.equals(null)) {
			throw new IllegalArgumentException("null object ");
		}
		if (!olddata.getClass().toString().equals(newdata.getClass().toString())) {
			throw new IllegalArgumentException("different objects");
		}
		List<ChangeDescription> changeslist = new ArrayList<ChangeDescription>();
		String[] szStrings = olddata.getClass().toString().split("\\.");
		String szproperfile = szStrings[szStrings.length-1] + "-comparator.properties";
		List<String> properslist = readProperties(szproperfile);
		try {
			for (String szproper : properslist) {
				Object oldval;
				Object newval;
				if (szproper.contains(".")) {
					oldval = PropertyUtils.getNestedProperty(olddata, szproper);
					newval = PropertyUtils.getNestedProperty(newdata, szproper);
				}
				else {
					oldval = PropertyUtils.getSimpleProperty(olddata, szproper);
					newval = PropertyUtils.getSimpleProperty(newdata, szproper);
				}
				if (!oldval.equals(newval)) {
					ChangeDescription changeDesc = new ChangeDescription(szproper, oldval.toString(), newval.toString());
					changeslist.add(changeDesc);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return changeslist;
	}
}
